import amaret.freestyle.board.Led0;
import amaret.freestyle.board.Led1;

import amaret.freestyle.mcu.msp430x22x4.BusyWait;
import amaret.freestyle.mcu.msp430x22x4.gpios.P12;

module Board inherits embios.interfaces.IBoard {

}

implementation {

    native {

        extern void xdc_runtime_System_rtsExit__I();
        
        #pragma FUNC_EXT_CALLED(abort);
        #pragma FUNC_EXT_CALLED(atexit);
        #pragma FUNC_EXT_CALLED(exit);
        
        void abort(void)
        {
           __asm("        .global C$$EXIT");
           __asm("C$$EXIT: nop");

           for (;;);   /* spin forever */
        }
        
        int atexit(void (*fxn)(void))
        {
            return (0);
        }
        
        void exit (int status)
        {
            xdc_runtime_System_rtsExit__I();
            abort();
        }
    }

    function startup()
    {
        // Configure Pin P1.2 with internal pullup resisters on.
        // This is for the board's button!!
        P12.internalPullupEnable(true);
        P12.makeInput();
    
        for (var Int i = 0; i < 4; i++) {
            BusyWait.wait(100000);
            Led1.toggle();
        }
    }
    
    function shutdown( stat )
    {
        if (stat == -1) { // abort status
            Led0.on();
        }
        else {          // some other status
            Led1.on();
        }
    }

}
